home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / earthmap / _earthmap / maps / getpar / getpar_sca < prev    next >
Encoding:
Text File  |  1989-04-01  |  5.2 KB  |  230 lines

  1. %C
  2. NONWHITE    [^ \t\n]
  3. ALPHA    [A-Za-z]
  4. ALPHANUM    [A-Za-z0-9_-]
  5. TAG    {ALPHA}{ALPHANUM}*=
  6. SQ    \'([^'\n]*\'\')*[^'\n]*['\n]
  7. DQ    \"([^"\n]*\"\")*[^"\n]*["\n]
  8. %{
  9. /* lexical scanning for fast getpar */
  10. /* Revised 3-8-86 stew  Added time stamp to enable older method of handling
  11.  *            multiple tags.  Moved par= intiialization to
  12.  *            separate routine to avoid code duplication.
  13.  */
  14. #include <ctype.h>
  15. #include "fastpar.h"
  16. #undef input
  17. #define input() ((int) *(input_stack[input_depth]++))
  18. #undef unput
  19. /* The redundant  =(c) insures side effects of expressions occur */
  20. #define unput(c) (*(--(input_stack[input_depth]))=(c))
  21. #define yywrap() getpar_pop_input()
  22. #define yylex() getpar_lexscan()
  23. #define yylook() getpar_yylook()
  24.  
  25. #define MAX_INPUT_DEPTH 10
  26. static int input_depth = -1;
  27. static char *input_stack[MAX_INPUT_DEPTH];
  28. static char *dealloc_stack[MAX_INPUT_DEPTH];
  29.  
  30. static struct {
  31.     char *tag;  int tlen;
  32.     char *val;  int vlen;
  33.        } yy;
  34.  
  35.  
  36. static int  SMALLBLOCK = 4096;
  37. static char
  38. *suballoc (size)
  39. int size;
  40. {
  41.     static char *myblock = (char *) NULL; static int bytesleft = 0;
  42.     char *ptr; extern char *alloc();
  43.  
  44.     if(size > SMALLBLOCK) return(alloc(size));
  45.     else
  46.       { 
  47.         if(bytesleft < size)
  48.            {
  49.             myblock = alloc (SMALLBLOCK);
  50.         bytesleft = SMALLBLOCK - size;
  51.            }
  52.        else
  53.            {
  54.         bytesleft -= size;
  55.            }
  56.        ptr = myblock;
  57.        myblock += size;
  58.        return(ptr);
  59.       }
  60. }
  61.  
  62. static int  prime[10] = {31,29,23,19,17,13,11,7,5,3};
  63. int getpar_hash(array,len)
  64. register char *array;
  65. register int len;
  66. {
  67.   register int hash;
  68.   register int i;
  69.   if(len >10) len=10;
  70.   hash=0;
  71.   for(i=0; i<len; i++)
  72.     hash += array[i]*prime[i];
  73.   return(hash);
  74. }
  75.  
  76. /* workhorse to decode par files; shell already parses command line */
  77. getpar_scan(queue,qlen)
  78. register hash_item **queue;
  79. register int qlen;
  80. {
  81.  extern int yylex();
  82.  
  83.  while(yylex()) {
  84.     getpar_hash_store(queue,qlen,yy.tag,yy.val,yy.tlen,yy.vlen);
  85.     if(yy.tlen == 3 && 0 == bcmp(yy.tag,"par",3))
  86.         getpar_stack_par(yy.val);
  87.     }
  88. }
  89.  
  90. /* read parfile into core and put buffer on scan input stack */
  91. getpar_stack_par(val)
  92. char *val;
  93. {
  94.  register char *buffer;
  95.  register int fd, len;
  96.  extern int file(), fsize();
  97.  extern char *alloc();
  98.  
  99.     fd = file(val,0);
  100.     len = fsize(fd);
  101.     buffer=alloc(len+3);
  102.     buffer[0]='\n';
  103.     read(fd,buffer+1,len);
  104.     buffer[len+1]='\n';
  105.     buffer[len+2]='\0';
  106.     getpar_push_input(buffer,1);
  107.     close(fd);
  108. }
  109.  
  110.  /* return 1 if match; 0 otherwise */
  111. #define getpar_hash_compare(next1,tag1,tlen1)  \
  112.  ((next1)->tlen == (tlen1) && 0 == bcmp((next1)->tag,tag1,tlen1))
  113.  
  114. getpar_hash_store(q,qlen,tag,val,tlen,vlen)
  115. hash_item **q;
  116. register char *tag, *val;
  117. register int tlen;
  118. int qlen, vlen;
  119. {
  120.  register hash_item *hold, *next;
  121.  static int storetime = 0;
  122.  
  123.  hold=(hash_item *) (q+getpar_hash(tag,tlen)%qlen);
  124.  next=hold->next;
  125.  
  126.  while(next != ((hash_item *) NULL)) {
  127.     if(getpar_hash_compare(next,tag,tlen) ) {
  128.         next->val = val; next->vlen = vlen;
  129.         next->timestamp = storetime++; return;
  130.         }
  131.     hold = next; next = next->next;
  132.     }
  133.  
  134.  hold->next = next = (hash_item *) suballoc(sizeof(hash_item));
  135.  next->next = (hash_item *) NULL;
  136.  next->tlen = tlen;
  137.  next->tag = tag;
  138.  next->vlen = vlen;
  139.  next->val = val;
  140.  next->timestamp = storetime++;
  141. }
  142.  
  143. hash_item *getpar_hash_lookup(q,qlen,tag,tlen)
  144. register hash_item **q;
  145. register char *tag;
  146. register int tlen;
  147. register int qlen;
  148. {
  149.  register hash_item *next;
  150.  
  151.  next = *(q + getpar_hash(tag,tlen)%qlen);
  152.  
  153.  while(next != ((hash_item *) NULL) ) {
  154.     if(getpar_hash_compare(next,tag,tlen)) break;
  155.     next = next->next;
  156.     }
  157.  return(next);
  158. }
  159.  
  160. %}
  161. %S FOUNDTAG
  162. %%
  163. <FOUNDTAG>{SQ}        {
  164.              yy.vlen = yyleng-2; yy.val=suballoc(yy.vlen+1);
  165.              yy.vlen = massage(yytext+1,yy.val,yy.vlen,yytext[0]);
  166.              yy.val[yy.vlen]='\0'; BEGIN 0; return(FOUNDTAG);
  167.              }
  168. <FOUNDTAG>{DQ}        {
  169.              yy.vlen = yyleng-2; yy.val=suballoc(yy.vlen+1);
  170.              yy.vlen = massage(yytext+1,yy.val,yy.vlen,yytext[0]);
  171.              yy.val[yy.vlen]='\0'; BEGIN 0; return(FOUNDTAG);
  172.              }
  173. <FOUNDTAG>[^'"]{NONWHITE}*    {
  174.                  yy.vlen=yyleng; yy.val=suballoc(yy.vlen+1);
  175.                   bcopy(yytext,yy.val,yy.vlen+1); BEGIN 0;
  176.                  return(FOUNDTAG);
  177.                  }
  178. ^{TAG}/{NONWHITE}    {
  179.              yy.tlen=yyleng-1; yy.tag=suballoc(yy.tlen+1);
  180.              bcopy(yytext,yy.tag,yy.tlen);
  181.              yy.tag[yy.tlen]='\0'; BEGIN FOUNDTAG;
  182.              }
  183. ([ \t]{TAG})/{NONWHITE}    {
  184.              yy.tlen=yyleng-2; yy.tag=suballoc(yy.tlen+1);
  185.              bcopy(yytext+1,yy.tag,yy.tlen);
  186.              yy.tag[yy.tlen]='\0'; BEGIN FOUNDTAG;
  187.              }
  188. ^\#.*    /* skip comment lines */;
  189. .    |
  190. \n    ;
  191. %%
  192.     getpar_push_input(buffer,dealloc)
  193.     register char *buffer;
  194.     register int dealloc;
  195.     {
  196.       if(input_depth++ == MAX_INPUT_DEPTH)
  197.         err("too many nested par files\n");
  198.       input_stack[input_depth] = buffer;
  199.       if(dealloc) dealloc_stack[input_depth] = buffer;
  200.       else dealloc_stack[input_depth] = (char *) NULL;
  201.     }
  202.  
  203.     int
  204.     yywrap()
  205.     {
  206.       if(((char *) NULL) != dealloc_stack[input_depth]) {
  207.         free(dealloc_stack[input_depth]);
  208.         dealloc_stack[input_depth] = (char *) NULL;
  209.         }
  210.       input_stack[input_depth--] = (char *) NULL;
  211.       if(input_depth < 0) return(1);
  212.       return(0);
  213.     }
  214.  
  215.     static int
  216.     massage(string,out,len,quote)
  217.     register char *string, *out;
  218.     register int len, quote;
  219.     {
  220.      register int i,j;
  221.     
  222.     for(i=0,j=0; i<len-1; j++) {
  223.         out[j]=string[i++];
  224.         if(out[j]==quote) /* compress doubled quotes */
  225.             if(string[i]==quote) i++;
  226.         }
  227.     if(i<len) out[j++] = string[i];
  228.     return(j);
  229.     }
  230.